package midalgorithms.treeandmap;

public class _05KthSmallest {
    public static int kthSmallest(TreeNode root, int k) {
        int[] values = new int[k];
        inorderTraversal(root,values,k);
        return values[k - 1];
    }

    static int index = 0;
    private static void inorderTraversal(TreeNode root, int[] values, int k) {
        if (root == null){
            return;
        }
        inorderTraversal(root.left,values,k);
        if (index > k-1){
            return;
        }
        values[index++] = root.val;
        inorderTraversal(root.right,values,k);
    }
}
